// index.js
// 获取应用实例
const app = getApp();
const QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
const util = require("../../utils/util");
import Toast from "../../vant/weapp/toast/toast";
let qqmapsdk;
Page({
  data: {
    rightArr: [{
      door_list: [{
        door_name: "开门"
      }],
    }],
    addIndex: 0,
    show: false,
    headTop: "",
    headHeight: "",
    sumHeight: "",
    diy: 2,
    imgUrls: [],
    alliancearr: [],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    swiperIndex: 0, // 第一次加载默认第一张图片选中
    latitude: 0,
    longitude: 0,
    store_type:1,
    store_name: '',
    city: '长沙市', //城市名称
    indexList: [],
    is_user_show: true,
    is_user: 0,
    showUser: false,
    isOrderList: true,
    // recomLists: [],
    cardList: [{
        url: "/image/card2.png",
        name: "棋牌",
        link: "/pages/storelist/index?store_type=1",
      },
      {
        url: "/image/card2.png",
        name: "KTV",
        link: "pages/index/storeselect/index?store_type=2",
      },
      {
        url: "/image/card2.png",
        name: "台球",
        link: "pages/index/storeselect/index?store_type=3",
      },
      {
        url: "/image/card2.png",
        name: "影吧",
        link: "/pages/index/storelist/index?store_type=4",
      },
    ],

    recomList: [],
    page: 1,
    // i: 0
  },
  onLoad(option) {
    console.log(option.store_type)
    const that = this;
    // 小程序相关参数
    qqmapsdk = new QQMapWX({
      key: 'YN6BZ-BITW3-QGM3R-3DECZ-MAUZH-DBFDA'
    });
    that.setData({
      headTop: app.globalData.headTop,
      headHeight: app.globalData.headHeight,
      sumHeight: app.globalData.sumHeight,
      store_type:option.store_type
    });

  },


  onShow: function () {
    const that = this
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        wx.setStorageSync('latitude', res.latitude)
        wx.setStorageSync('longitude', res.longitude)
      }
    });
    that.data.page = 1
    that.init()
    // that.getUserOrderInfo()
  },
  onReachBottom() {
    const that = this
    that.data.page++
    app.http.get(
      app.api.index, {
        lat: wx.getStorageSync('latitude') || 0,
        lng: wx.getStorageSync('longitude') || 0,
        title: that.data.store_name,
        city_code: that.data.city,
        page: that.data.page
      }, (res) => {
        if (res.data == "") {
          that.setData({
            recomList: res.data.lists.data,
            isOrderList: true
          })
        } else {
          let recomList = this.data.recomList.concat(res.data.lists.data);
          wx.setStorageSync('recomList', recomList)
          this.setData({
            recomList,
            isOrderList: false,
          })

        }
      }
    )

  },
  //   登录
  login(e) {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    }
  },
  // 登录遮罩层
  close(e) {
    console.log(e)
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    this.setData({
      showUser: true,
    });
    wx.setStorageSync('userInfo', e.detail)
  },



  //   门店列表
  init() {
    const that = this
    that.data.recomList = null
    app.http.get(
      app.api.storetype, {
        lat: wx.getStorageSync('latitude') || 0,
        lng: wx.getStorageSync('longitude') || 0,
        title: that.data.store_name,
        store_type: that.data.store_type,
        city_code: that.data.city,
        page: that.data.page
      }, (res) => {
        if (res.data.lists.data == "") {
          that.setData({
            recomList: res.data.lists.data,
            isOrderList: true
          })
        } else {
          that.setData({
            isOrderList: false,
            recomList: res.data.lists.data
          })
        }
      }
    )
  },





  getLocal(latitude, longitude) {
    let that = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: Number(latitude),
        longitude: Number(longitude)
      },
      success: function (res) {
        let city = res.result.ad_info.city
        if (city == wx.getStorageSync('city')) {
          return
        }
        that.setData({
          city,
          latitude,
          longitude
        })
        wx.setStorageSync('city', city)
        that.init();
      },

      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },

  onPullDownRefresh: function () {
    this.onRefresh();
  },
  onRefresh: function () {
    //导航条加载动画
    wx.showNavigationBarLoading()
    //loading 提示框
    wx.showLoading({
      title: 'Loading...',
    })

    this.Show();
    setTimeout(function () {
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000)
  },
});